import api from "../../utils/api"

// pages/person/person.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {} as UserProfile,//用户信息
    couponCount: {} as CouponCounts,//卡卷数量
    messageCount: 0 as number,//消息数量
    orderCount: {} as OrderCounts,//订单数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
  },
  // 修改用户信息
  changeInfo(): void {
    if (this.data.userInfo && this.data.userInfo.phone) {
      wx.navigateTo({
        url: "/pages/editinfo/editinfo?nickName=" + (this.data.userInfo.nickname || '') + '&avatar=' + (this.data.userInfo.avatar || '')
      })
      return;
    }
    let loginpop = this.selectComponent('#loginpop');
    loginpop.show(() => {
      this.onLoad();
      this.onShow();
    }, () => {
      //do nothing
    });
  },
  toOrderInfo(e: any): void {
    const status = e.currentTarget.dataset.status
    wx.navigateTo({
      url: "/pages/orderinfo/orderinfo?status=" + status
    })
  },
  toAbout(): void {
    wx.navigateTo({
      url: "/pages/about/about?title=0"
    })
  },
  toNotice(): void {
    wx.navigateTo({
      url: "/pages/notice/notice"
    })
  },
  toCardBag(): void {
    wx.navigateTo({
      url: "/pages/card/card"
    })
  },
  toCouponCollect(): void {
    wx.navigateTo({
      url: '/pages/couponcollect/couponcollect'
    })
  },
  toCami(): void {
    wx.navigateTo({
      url: "/pages/cami/cami"
    })
  },
  async toPublic(): Promise<void> {
    const status = await this.getResellerInfo()
    // const status = "NONE"
    // const status = "APPLIED"
    // const status = "REJECTED"
    if (status != "IS_RESELLER") {
      wx.navigateTo({
        url: "/pages/invite/invite?status=" + status
      })
    } else {
      wx.navigateTo({
        url: "/pages/public/public"
      })
    }
  },
  toAddress(): void {
    wx.navigateTo({
      url: '/pages/address/address'
    })
  },
  toQuestion(): void {
    wx.navigateTo({
      url: "/pages/question/question"
    })
  },
  // 获取用户信息列表
  getProfile(): void {
    api.user.getProfile().then(res => {
      if (res.data.code == '000') {
        if (res.data.data && res.data.data.phone) {
          res.data.data.phone = res.data.data.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
        }
        console.log(res.data.data);
        let userInfo;
        if (res.data.data) {
          userInfo = res.data.data;
        } else {
          userInfo = {};
        }
        this.setData({
          userInfo: userInfo
        })
      } else {
        wx.showToast({
          title: res.data.message as string,
          icon: "error"
        })
      }
    })
  },
  // 获取订单数量
  getOrderCounts(): void {
    api.user.getOrderCounts().then(res => {
      if (res.data.code == "000") {
        this.setData({
          orderCount: res.data.data
        })
      } else {
        wx.showToast({
          title: res.data.message as string,
          icon: "error"
        })
      }
    })
  },
  // 获取优惠卷适量
  getCouponCounts(): void {
    api.user.getCouponCounts().then(res => {
      if (res.data.code == "000") {
        this.setData({
          couponCount: res.data.data
        })
      } else {
        wx.showToast({
          title: res.data.message as string,
          icon: "error"
        })
      }
    })
  },
  // 获取消息数量
  getMessageCount(): void {
    api.user.getMessageCount().then(res => {
      if (res.data.code == "000") {
        this.setData({
          messageCount: res.data.data
        })
      } else {
        wx.showToast({
          title: res.data.message as string,
          icon: "error"
        })
      }
    })
  },
  // 分销信息
  async getResellerInfo(): Promise<String> {
    const res = await api.reseller.getResellerInfo()
    if (res.data.code == "000") {
      if (res.data.data) {
        return res.data.data.status
      }
    } else {
      wx.showToast({
        title: res.data.message as string,
        icon: "error"
      })
    }
    return "NONE"
  },
  // 退出登录
  async logout(): Promise<void> {
    wx.showModal({
      content: '确定退出登录吗？'
    }).then(confirm => {
      if (!confirm.confirm) {
        return;
      }
      api.login.logout().then(() => {
        this.onLoad();
        this.onShow();
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getProfile();
    this.getCouponCounts();
    this.getOrderCounts();
    this.getMessageCount();
  },
})